home *** CD-ROM | disk | FTP | other *** search
- Path: hermes.ucd.ie!usenet
- From: Luca Piccarreta <es-46@scolaire.ucd.ie>
- Newsgroups: comp.lang.c++
- Subject: Programming problem with Borland C++ 3.1
- Date: Wed, 21 Feb 1996 15:57:13 +0000
- Organization: University College Dublin
- Message-ID: <312B40D9.3E66@scolaire.ucd.ie>
- NNTP-Posting-Host: luca.ucd.ie
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (Win95; I)
-
- Hi, I'm Luca Piccarreta, Italian Student temporarily in Ireland under
- ERASMUS program.
- I'm doing a program about speech Recognition and I've run into a problem
- that I'm not able to solve, because I simply don't understand it.
- I wrote this code:
-
- template <class T>
- class vector{
- public:
- virtual unsigned GetDim()const=0;
- virtual T operator[](unsigned i)const=0;
- virtual T& operator[](unsigned i)=0;
-
- int operator ==(const vector<T>& v);
- virtual vector<T>& operator -=(const vector<T>& v);
- virtual vector<T>& operator +=(const vector<T>& v);
- virtual vector<T>& operator =(const vector<T>& v);
- virtual vector<T>& Set(const vector<T>& v);
-
- virtual vector<T>& operator *=(const vector<T>& mul);
- /* this is a product term by term, is not a real vector op
- virtual vector<T>& operator *=(T mul);
- virtual vector<T>& operator /=(T div);
- virtual T operator *(const vector<T>& mul);
- };
-
- template <class T>
- class vectorArr:public vector<T>{
- int dim;
- T* data;
- public:
- vectorArr(int dim=0);
- vectorArr(const vectorArr<T>& v);
- ~vectorArr();
- unsigned GetDim()const
- {
- return dim;
- }
- void SetDim(unsigned _dim);
- T operator[](unsigned i)const{
- PRECONDITION(i<dim);
- return data[i];
- }
- T& operator[](unsigned i){
- PRECONDITION(i<dim);
- return data[i];
- }
- }
-
- Now, I won't write all the code for these two classes, but I have a
- problem with the overloaded operator=. That is:
-
-
- template <class T>
- vector<T>& vector<T>::operator =(const vector<T>& v)
- {
- PRECONDITION(GetDim()==v.GetDim());
- for(int i=0;i<GetDim();i++)
- {
- (*this)[i]=v[i];
- }
- return *this;
- }
- template <class T>
- vector<T>& vector<T>::Set(const vector<T>& v)
- {
- PRECONDITION(GetDim()==v.GetDim());
- for(int i=0;i<GetDim();i++)
- {
- (*this)[i]=v[i];
- }
- return *this;
- }
-
- these two functions should do exactly the same job, as far as I know,
- but they don't. why?
- If I try and use the operator= (between two elements of the derived
- class vectorArr) the program fails, and, by debugging, I discovered that
- the compiler generates different codes not for the functions, but at the
- point where the functions are called (they are not inlined).
- Can any guru or half-guru help me?
-
- Thanks in advance (forgive my poor english).
-
- Luca Piccarreta
- es-46@scolaire.ucd.ie
-